import 'dart:convert';
import 'package:flutter/material.dart';
import './model/license.dart';
import './widget/license_widget.dart';

class LicenseCustomPage extends StatelessWidget {
  const LicenseCustomPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('License'),
        centerTitle: true,
      ),
      body: FutureBuilder<List<License>>(
        future: loadLicenses(context),
        builder: (context, snapshot) {
          final license = snapshot.data;
          switch (snapshot.connectionState) {
            case ConnectionState.waiting:
              return const Center(child: CircularProgressIndicator());
            default:
              if (snapshot.hasError) {
                return const Center(child: Text('Some error occurred!'));
              } else {
                return LicenseWidget(licenses: license!);
              }
          }
        }
      ),
    );
  }

  Future<List<License>> loadLicenses(BuildContext context) async {
    final bundle = DefaultAssetBundle.of(context);
    final licenses = await bundle.loadString('assets/licenses.json');
    return json.decode(licenses).map<License>((license) => License.fromJson(license)).toList();
  }
}